package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.HouseInfoMapper;
import com.wuming.house.model.HouseInfo;
import com.wuming.house.service.HouseInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("houseInfoService")
public class HouseInfoServiceImpl extends BaseServiceImpl implements HouseInfoService {

    @Autowired
    private HouseInfoMapper houseInfoMapper;

    @Override
    public void saveHouseInfo(HouseInfo houseInfo) throws BusinessException {
        houseInfoMapper.insert(houseInfo);
    }

    @Override
    public List<HouseInfo> getHouseInfoByHouseId(Long houseId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("houseId", houseId);
        return houseInfoMapper.selectInfoByHouse(params);
    }

    @Override
    public void modifyHouseInfo(HouseInfo houseInfo) throws BusinessException {
        houseInfoMapper.updateByPrimaryKey(houseInfo);
    }

    @Override
    public void removeHouseInfoById(Long id) throws BusinessException {
        houseInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<HouseInfo> getHouseInfoByPage(Long houseId, String infoTitle, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (null != houseId) {
            params.put("houseId", houseId);
        }
        if (StringUtils.isNotBlank(infoTitle)) {
            params.put("infoTitle", infoTitle);
        }
        try {
            return (List<HouseInfo>) this.getPageList(HouseInfoMapper.class, "selectByParams", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询楼盘动态图失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(Long houseId, String infoTitle) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (null != houseId) {
            params.put("houseId", houseId);
        }
        if (StringUtils.isNotBlank(infoTitle)) {
            params.put("infoTitle", infoTitle);
        }
        try {
            return this.getTotal(HouseInfoMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("分页查询楼盘动态总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
